package de.tum.activities.StatsApp;



import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.R.color;
import android.content.Context;
import android.graphics.Color;

public class GradeDistributionBarChart {
		 
	public GraphicalView execute(Context context, int[] grade_distribution) {
		int[] colors = new int[grade_distribution.length];
		for(int i=0; i<colors.length; i++){
			if(i<4)
				colors[i] = Color.RED;
			else if(i<10)
				colors[i] = Color.YELLOW;
			else
				colors[i] = Color.GREEN;
		}
		XYMultipleSeriesRenderer renderer = buildCategoryRenderer(colors);
		 

		XYMultipleSeriesDataset categorySeries = new XYMultipleSeriesDataset();
		CategorySeries gradeSeries = new CategorySeries("Occurence of the specific grades");
		gradeSeries.add("1.0", grade_distribution[0]);
		gradeSeries.add("1.3", grade_distribution[1]);
		gradeSeries.add("1.7", grade_distribution[2]);
		gradeSeries.add("2.0", grade_distribution[3]);
		gradeSeries.add("2.3", grade_distribution[4]);
		gradeSeries.add("2.7", grade_distribution[5]);
		gradeSeries.add("3.0", grade_distribution[6]);
		gradeSeries.add("3.3", grade_distribution[7]);
		gradeSeries.add("3.7", grade_distribution[8]);
		gradeSeries.add("4.0", grade_distribution[9]);
		gradeSeries.add("4.3", grade_distribution[10]);
		gradeSeries.add("4.7", grade_distribution[11]);
		gradeSeries.add("5.0", grade_distribution[12]);
		
		categorySeries.addSeries(gradeSeries.toXYSeries());
		
		return ChartFactory.getBarChartView(context, categorySeries, renderer, Type.DEFAULT);
	}
		 
	protected XYMultipleSeriesRenderer buildCategoryRenderer(int[] colors) {
		XYMultipleSeriesRenderer XYMultipleRenderer = new XYMultipleSeriesRenderer();
		XYSeriesRenderer XYRenderer = new XYSeriesRenderer();

		XYRenderer.setColor(Color.BLUE);
	    XYRenderer.setPointStyle(PointStyle.SQUARE);
	    XYRenderer.setFillBelowLine(true);
	    XYRenderer.setFillBelowLineColor(Color.WHITE);
	    XYRenderer.setFillPoints(true);
	    
	    
	    XYMultipleRenderer.setBarSpacing(1);
	    XYMultipleRenderer.setXLabels(0);
	    XYMultipleRenderer.addXTextLabel(1, "1.0");
	    XYMultipleRenderer.addXTextLabel(2, "1.3");
	    XYMultipleRenderer.addXTextLabel(3, "1.7");
	    XYMultipleRenderer.addXTextLabel(4, "2.0");
	    XYMultipleRenderer.addXTextLabel(5, "2.3");
	    XYMultipleRenderer.addXTextLabel(6, "2.7");
	    XYMultipleRenderer.addXTextLabel(7, "3.0");
	    XYMultipleRenderer.addXTextLabel(8, "3.3");
	    XYMultipleRenderer.addXTextLabel(9, "3.7");
	    XYMultipleRenderer.addXTextLabel(10, "4.0");
	    XYMultipleRenderer.addXTextLabel(11, "4.3");
	    XYMultipleRenderer.addXTextLabel(12, "4.7");
	    XYMultipleRenderer.addXTextLabel(13, "5.0");
	    
	    XYMultipleRenderer.addSeriesRenderer(XYRenderer);
	  	    
		return XYMultipleRenderer;
	}
}